//
//  PopoverAnimater.swift
//  DSWeiBo
//
//  Created by Frank Wang on 7/8/16.
//  Copyright © 2016 Frank Wang. All rights reserved.
//

import UIKit


//定义常量保存通知的名称
let XMGPopoverAnimaterWillShow = "XMGPopoverAnimaterWillShow"
let XMGPopoverAnimaterWillDismiss = "XMGPopoverAnimaterWillDismiss"


class PopoverAnimater: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {

        var isPresent: Bool = false
        
        /// 定义属性保存菜单大小。
        var presentFrame = CGRectZero
        
        //通过分类来设置专场动画
        //        extension HomeTableViewController : UIViewControllerTransitioningDelegate{
        //                presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
        //        }
        //ios 8 专门用于负责专场动画的
        func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
                
                let pc = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
                //设置菜单大小
                pc.presentFrame = presentFrame
                return pc
                
        }
        
        
        
        /**
         //谁负责Modal的展现
         
         - parameter presented:  被展现的视图
         - parameter presenting: 发起的视图
         - parameter source:     <#source description#>
         
         - returns: 谁来负责
         */
        
        //只要实现了以下方法，系统默认的动画就不会执行了
        func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
                isPresent = true
                
                //发送通知，通知控制器箭头。
                NSNotificationCenter.defaultCenter().postNotificationName(XMGPopoverAnimaterWillShow, object: self)
                
                return self
                
        }
        
        //谁负责Modal的消失
        func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
                isPresent = false
                
                NSNotificationCenter.defaultCenter().postNotificationName(XMGPopoverAnimaterWillDismiss, object: self)
                
                return self
        }
        
        func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
                //动画时长，统一数值。
                return 0.5
        }
        
        func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
                
                //1.修改transfrom
                //                let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
                //                let fromVC  = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
                if isPresent {
                        //代表已经展开
                        let toVC = transitionContext.viewForKey(UITransitionContextToViewKey)
                        toVC?.transform = CGAffineTransformMakeScale(1.0, 0.0);
                        //执行动画
                        //注意一定要视图添加到容器上
                        transitionContext.containerView()?.addSubview(toVC!)
                        
                        //设置猫店
                        toVC?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
                        
                        UIView .animateWithDuration(transitionDuration(transitionContext), animations: {
                                //清空transform
                                toVC?.transform = CGAffineTransformIdentity
                        }) { (_) in
                                //动画完成后，一定要通知系统
                                transitionContext.completeTransition(true)
                        }
                        
                }else{
                        //代表需要消失
                        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
                        UIView .animateWithDuration(transitionDuration(transitionContext), animations: {
                                //由于CGFloat时不准确的，所以如果写0.0会没有动画
                                
                                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                                }, completion: { (_) in
                                        transitionContext.completeTransition(true)
                        })
                        
                        
                }
                
                
                
                
        }
        

}
